package com.xiyoukt.springboot.controller;

import com.xiyoukt.springboot.Result;
import com.xiyoukt.springboot.User;
import com.xiyoukt.springboot.service.UserService;
import com.xiyoukt.springboot.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    // POST
    @GetMapping("addUser")
    public Result<User> addUser(User user){
        ResultUtil<User> resultUtil = new ResultUtil<>();
        User resultUser = userService.addUser(user);
        return resultUtil.success(resultUser);
    }

    @GetMapping("getAll")
    public Result<List<User>> getAll(){
        ResultUtil<List<User>> resultUtil = new ResultUtil<>();
        List<User> all = userService.getAll();
        return resultUtil.success(all);
    }

    @GetMapping("delUser")
    public Result<Boolean> delUser(Long id){
        ResultUtil<Boolean> resultUtil = new ResultUtil<>();
        userService.delUser(id);
        return resultUtil.success(Boolean.TRUE);
    }

    @GetMapping("getUser")
    public Result<List<User>> getUser(Integer gender){
        ResultUtil<List<User>> resultUtil = new ResultUtil<>();
        List<User> user = userService.findUserByGender(gender);
        return resultUtil.success(user);
    }

}
